﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Kinban.Contracts;

namespace DemoTfsUtility
{
    public class DemoTfsAccess : ITfsGateway
    {
        private List<Story> _stories;
        private readonly string _sampleDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SampleData.xml");

        public DemoTfsAccess()
        {
            var serializer = new XmlSerializer(typeof(List<Story>));
            using (XmlReader xmlReader = new XmlTextReader(_sampleDataPath))
            {
                _stories = (List<Story>)serializer.Deserialize(xmlReader);
            }
        }

        public List<Story> GetStories(List<string> projectNames)
        {
            return _stories;
        }

        public void UpdateStory(Story story)
        {
            var targetStory = _stories.Single(x => x.Id == story.Id);
            _stories.Remove(targetStory);
            _stories.Add(story);

            var serializer = new XmlSerializer(typeof(List<Story>));
            using (XmlWriter writer = new XmlTextWriter(_sampleDataPath, Encoding.ASCII))
            {
                serializer.Serialize(writer, _stories);
            }
        }
    }
}
